#!/usr/bin/python
#-*-coding: utf-8 -*-

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from optparse import make_option
from handler import HandlerPart

from pylot.core import ENCODING
from pylot.cli import CliInterface, pyLotOptionGroup

class CliPart(CliInterface):

  def __init__ (self, handler=None, parser=None):
    CliInterface.__init__(self, handler=handler, parser=parser)

    self.version = '0.1'
    self.description = self.tr_(u'A tool to translate plugins')
    self._createOptions()

  def _createOptions(self):
    self.action_init_translation = make_option('--init-translation',
      action='store_true', dest='init_translation')
    self.action_update_translation = make_option('--update-translation',
      action='store_true', dest='update_translation')
    self.action_compile_translation = make_option('--compile-translation',
      action='store_true', dest='compile_translation')

    self.action_plugin_path = make_option('--plugin-path',
      action='store', dest='plugin_path')
    self.action_locale = make_option('--locale',
      action='store', dest='locale')

  def _createOptionGroups(self):
    group = pyLotOptionGroup(self.parser, u'group_translate')
    self.group_translate = self.addGroup(group)

  def _fillGroups(self):
    group = self.group_translate
    group.add_option(self.action_init_translation,
      userlist=self.parser.console)
    group.add_option(self.action_update_translation,
      userlist=self.parser.console)
    group.add_option(self.action_compile_translation,
      userlist=self.parser.console)
    group.add_option(self.action_locale)
    group.add_option(self.action_plugin_path)

  def _runOptions(self, options):
    # action for i18n (special CLI treatments)
    self.autoRunOption(self.action_locale,
                       self._onActionLocale)
    self.autoRunOption(self.action_plugin_path,
                       self._onActionPluginPath)

    # action for i18n (direct calls to handler)
    self.autoRunOption(self.action_init_translation,
                       self.handler.actionInitTranslation)
    self.autoRunOption(self.action_update_translation,
                       self.handler.actionUpdateTranslation)
    self.autoRunOption(self.action_compile_translation,
                       self.handler.actionCompileTranslation)

  def _onActionLocale(self):
    locale = self.options.locale.decode(ENCODING)
    self.handler.opt[u'translation'][u'lang'] = locale

  def _onActionPluginPath(self):
    path = Path(self.options.plugin_path.decode(ENCODING)).abspath()
    self.handler.opt[u'lastpaths'][u'plugins'] = path

  def _translate(self):
    self._setTexts([
      (self.group_translate, self.tr_(u'Command useful for plugin translations'))
      ])

    self._setTexts([
      (self.group_translate, self.tr_(u'Translation options')),
      ], field=u'title')

if __name__ == '__main__':
  u"""
  Launching CLI
  """
  from _pylot.cli.tools.utils import launch_cli_module
  parser, cli = launch_cli_module(CliPart)
